package com.action.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.action.childbase.ChildBaseFragment;
import com.action.entity.UserCommentVO;
import com.action.service.pressenter.RedpakcetPressenter;
import com.action.ui.activity.RedPacketDetail;
import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.model.Paginated;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.xinglai.yun.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.Collection;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class RedDetailCommentListFrag extends ChildBaseFragment<RedpakcetPressenter> implements ActionPacketView.onPostComment {


    @BindView(R.id.comment_container)
    XRecyclerView commentContainer;


    private RedUserCommentAdapter redUserCommentAdapter;
    private Boolean hasMore = true;

    public RedDetailCommentListFrag() {
    }

    private int pageNo = 1;

    public RedUserCommentAdapter getRedUserCommentAdapter() {
        return redUserCommentAdapter;
    }

    @Override
    protected void initView() {
        super.initView();
        redUserCommentAdapter = new RedUserCommentAdapter();

        commentContainer.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        commentContainer.setPullRefreshEnabled(false);
        commentContainer.setLoadingMoreEnabled(true);
        commentContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                if (!hasMore) {
                    return;
                }
                pageNo += 1;
                fetchCommentList();
            }
        });
        commentContainer.setAdapter(redUserCommentAdapter);
        commentContainer.setEmptyView(getEmptyView(commentContainer, "快来抢沙发吧", "", null));
    }


    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    @Override
    protected boolean isNeedShowLoading() {
        return true;
    }

    @Override
    protected boolean needShowError() {
        return true;
    }

    @Override
    protected void loadData() {
        super.loadData();
        pageNo = 1;
        fetchCommentList();
    }

    public void fetchCommentList() {
        if (null != mPresenter) {
            mPresenter.fetchCommentList(sn, pageNo);
        }
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    private String sn;

    public void setRedpacketSn(String redpacketId) {
        this.sn = redpacketId;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_red_detail_comment_list;
    }

    @Override
    public void onCommentSuccess() {
        dismissProgress();
        showToast("评价成功");
        fetchCommentList();
        ((RedPacketDetail) getActivity()).setCommonSuccessAfter();
    }

    public static class RedUserCommentAdapter extends RecyclerAdapter<UserCommentVO, RedUserCommentAdapter.ViewHolder> {

        @Override
        protected void fillData(UserCommentVO item, RedUserCommentAdapter.ViewHolder holder) {
            XYBAUtils.displayCircleImage(holder.itemView.getContext(), item.getWxIcon(), holder.userImage);
            holder.commentName.setText(item.getWxNickname());
            holder.commentContent.setText(item.getComment());
            holder.commentDate.setText(item.getCommentTime());
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.unit_user_comment_redpacket;
        }

        public class ViewHolder extends RecyclerViewHolder {
            @BindView(R.id.user_image)
            public ImageView userImage;
            @BindView(R.id.comment_name)
            public TextView commentName;
            @BindView(R.id.comment_content)
            public TextView commentContent;
            @BindView(R.id.comment_date)
            public TextView commentDate;

            public ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    public void postComment(String comment) {
        if (null != mPresenter) {
            showProgress();
            mPresenter.postComment(comment, sn);
        }
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof Collection) {
            if (tClass == UserCommentVO.class) {
                List<UserCommentVO> o = (List<UserCommentVO>) object;
                if (redUserCommentAdapter == null || commentContainer == null) {
                    return;
                }
                if (pageNo == 1) {
                    redUserCommentAdapter.clear();
                }

                redUserCommentAdapter.addAll(o);
                redUserCommentAdapter.notifyDataSetChanged();
                if (commentContainer != null) {
                    hasMore = paginated.getHasMore();
                    commentContainer.setLoadingMoreEnabled(hasMore);
                }

            }
        }
        setState(AppConstants.STATE_SUCCESS);
    }
}
